#from Mqtt import mqtt
import concurrent.futures

import requests
from concurrent.futures import ThreadPoolExecutor
from mysqltest.mysql_db import sqlconnect
from tools.get_headerjson import GetcJson
from conf.api_path import ApiPath
from tools.get_headerjson import GetHeaderjson
from tools.logger import log  # 导入封装的logger方法
import paho.mqtt.client as mqtt
import requests
import os
import time
import json



ApiPath = ApiPath()
GetcJson = GetcJson()
GetHeaderjson = GetHeaderjson()
ids = []


def audiostop_post_request(device_id):

  GetcJson.set_audiostop_deviceId(device_id)
  response = requests.post(url=ApiPath.audio_stop,
                        headers=GetHeaderjson.get_s_headers(),
                        json=GetcJson.get_audiostop())
  print(f"1_Audiostop_Response for {device_id}: {response.status_code}")



def relay_post_request(device_id):
  GetcJson.set_relaymodel(1)
  GetcJson.set_relaysatus(1)
  GetcJson.set_relay_deviceId(device_id)
  response = requests.post(url=ApiPath.relay_Operate,
                           headers=GetHeaderjson.get_s_headers(),
                           json=GetcJson.get_relay())
  print(f"2_Relay_Response for {device_id}: {response.status_code}")



def volume_post_request(device_id):

  GetcJson.set_volume(6)
  GetcJson.set_volume_deviceId(device_id)
  response = requests.post(url=ApiPath.volume_set,
                           headers=GetHeaderjson.get_s_headers(),
                           json=GetcJson.get_volume())
  print(f"3_Volume_Response for {device_id}: {response.status_code}")


def device_Id():
    sql1 = "select device_code,device_id from device where is_del = 0  and dept_id = 1"
    tupl = sqlconnect.sqljudje(sql1)

    len1 = len(tupl)
    for row in tupl:
      le = len(tupl)
      # global device_id
      # dev_code = str(row[0])  # 假设column_name是第一列
      device_id = str(row[1])  # 假设column_name是第一列
      # print("打印deviceId:",device_id)
      ids.append(device_id)
    return ids



def  audiostop_post():
  try:
    device_Id()
    l = len(ids)
    #同时对10台设备进行请求
    executor = ThreadPoolExecutor(max_workers=l)
    try:
      for i in range(l):
        executor.submit(audiostop_post_request, ids[i])
        time.sleep(0.1)
        # 关闭执行器，不等待任务完成

      executor.shutdown(wait=False)
    except Exception as e:
      print(f"提交任务时出错(设备 {ids[i]}): {str(e)}")

  except Exception as e:
        print(f"relay_post() 主函数出错: {str(e)}")
        raise  # 可以选择重新抛出异常或处理


def  relay_post():
    #device_Id()
    print(ids)
    l = len(ids)
    #同时对10台设备进行请求
    executor = ThreadPoolExecutor(max_workers=l)
    for i in range(l):
      executor.submit(relay_post_request, ids[i])
      time.sleep(0.1)
      # 关闭执行器，不等待任务完成
    executor.shutdown(wait=False)


def volume_post():
  # device_Id()
  print(ids)
  l = len(ids)
  # 同时对10台设备进行请求
  executor = ThreadPoolExecutor(max_workers=l)
  for i in range(l):
    executor.submit(volume_post_request, ids[i])
    time.sleep(0.1)
    # 关闭执行器，不等待任务完成
  executor.shutdown(wait=False)

